package com.quanyan.authority.web;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.authority.model.db.TbAuthorityFunction;
import com.quanyan.authority.service.AuthorityFunctionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Administrator on 2016/8/5.
 */
@RestController
@RequestMapping("/admin/api/authority/authorityFun/api/")
public class AuthorityFunctionController {

    private static final Logger logger = LoggerFactory.getLogger(AuthorityFunctionController.class);

    @Autowired
    private AuthorityFunctionService authorityFunctionService;


    @RequestMapping(value = "add",method = RequestMethod.POST)
    public APIResponse addAuthorityFunction(@RequestBody @Valid List<TbAuthorityFunction> lst,BindingResult bindingResult){
        if(bindingResult.hasErrors())
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        try {
            authorityFunctionService.batchAddAuthorityFunction(lst);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
    }

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public APIResponse delAuthorityFunction(@RequestBody List<TbAuthorityFunction> ids){
        if(ids == null || ids.size() < 1)
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        try {
            authorityFunctionService.batchDelAuthorityFunction(ids);
            return APIResponse.returnSuccess();
        } catch (Exception e) {
            logger.error("{}",e);
            return APIResponse.returnFail("系统异常！");
        }
    }

}
